package com.example.rabbitmq;

import com.example.rabbitmq.test.processors.OrderProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;

@SpringBootApplication
@EnableBinding({Processor.class, OrderProcessor.class})
public class RabbitmqApplication implements CommandLineRunner {

    @Autowired
    @Qualifier("output")
    MessageChannel output;


    public static void main(String[] args) {
        SpringApplication.run(RabbitmqApplication.class);
    }

    // 监听 binding 为 processors.INPUT 的消息
    @StreamListener(Processor.INPUT)
    public void input(Message<String> message) {
        System.out.println("一般监听收到：" + message.getPayload());
    }

    @Override
    public void run(String... strings) throws Exception {
        // 字符串类型发送MQ
        System.out.println("字符串信息发送");
        output.send(MessageBuilder.withPayload("大家好").build());
    }

}
